<?php
namespace App\Model;

use App\Http\Model\Hint;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AdminIp extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'admin_ip';

    //后台ip白名单列表
    public function list(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);

        $result = DB::connection($this->connection)->table($this->table)
            ->where('is_delete', '0')
            ->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);

        if(!empty($result['data'])){
            foreach($result['data'] as $k => $v){
                $user = Users::find($v['limit_id']);
                $v['limit_name'] = $user ? $user->name : '';
                $result['data'][$k] = $v;
            }
        }

        return $result;
    }

    //后台ip白名单新增
    public function add($params, $admin){
        unset($params['id']);

        $res = [
            'code' => 0,
            'data' => [],
            'msg' => '成功',
        ];
        if(empty($params['ip']) || empty($params['limit_id'])) {
            $res['code'] = 1;
            $res['msg'] = 'ip empty';
            return $res;
        }
        $data['ip'] = $params['ip'];
        $data['limit_id'] = $params['limit_id'];
        $data['remark'] = $params['remark'] ?: '';
        $data['admin_id'] = $admin['id'];
        $data['admin_name'] = $admin['name'];
        $data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s', time());
        DB::connection($this->connection)->table($this->table)->insertGetId($data);
        return $res;
    }

    //后台ip白名单删除
    public function del($data){
        $res = [
            'code' => 0,
            'data' => [],
            'msg' => '成功',
        ];
        if(empty($data['id'])) {
            $res['code'] = 1;
            $res['msg'] = 'id empty';
            return $res;
        }
        DB::connection($this->connection)->table($this->table)->where('id', '=', $data['id'])->update(['is_delete' => 1]);
        return $res;
    }

    //后台ip白名单编辑
    public function status($data){
        $res = [
            'code' => 0,
            'data' => [],
            'msg' => '成功',
        ];
        if(empty($data['id']) || !isset($data['status'])) {
            $res['code'] = 1;
            $res['msg'] = 'param empty';
            return $res;
        }
        $id = $data['id'];
        $status = $data['status'];

        DB::connection($this->connection)->table($this->table)->where('id', '=', $id)->update(['status' => $status]);
        return $res;
    }

    public function checkIp($ip, $id) {
        $list = DB::connection($this->connection)->table($this->table)
            ->where('limit_id', $id)
            ->where('is_delete', 0)
            ->where('status', 1)
            ->get();
        $list = $this->objToArray($list);
        $ips = array_column($list, 'ip');
        if ($ips) {
            return in_array($ip, $ips);
        }
        return true;

    }


    public function optIpList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $adminId = $request->input('admin_id', '');
        $startTime = $request->input('start_time', '');
        $endTime = $request->input('end_time', '');

        $query = DB::connection($this->connection)->table('operation_logs');

        if (!empty($adminId)) {
            $query->where('admin_id', '=', $adminId);
        } else {
            $query->where('admin_id', '>', 0);
        }

        if (!empty($startTime))
            $query->where('created_at', '>=', $startTime);
        if (!empty($endTime))
            $query->where('created_at', '<=', $endTime);

        $lists = $query->selectRaw('admin_id, ip, MIN(created_at) as created_at')
            ->groupBy('admin_id', 'ip')
            ->paginate($limit, ['*'], 'page', $page);
        $lists = $this->objToArray($lists);

        $adminIds = array_column($lists['data'], 'admin_id');
        $adminList = DB::table('users')->whereIn('id', $adminIds)->select(['id','name'])->get();
        $adminList = array_column(objToArray($adminList), null, 'id');

        foreach($lists['data'] as $key=>$val) {
            $val['admin_name'] = empty($adminList[$val['admin_id']]['name']) ? '' : $adminList[$val['admin_id']]['name'];
            $lists['data'][$key] = $val;
        }

        return $lists;
    }

}
